package com.stedu.controller;

import com.stedu.bean.Account;
import com.stedu.bean.Admin;
import com.stedu.bean.RespBean;
import com.stedu.mapper.AccountMapper;
import com.stedu.service.AccountService;
import com.stedu.service.AdminService;
import com.stedu.utils.JwtUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;
import java.util.Map;

@CrossOrigin
@RestController
@RequestMapping("/admin/info")
public class InfoController {

    @Autowired
    private AccountService accountService;
    @Autowired
    private AdminService adminService;

    @GetMapping("/top5")
    public RespBean top5DnameAndCount(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> mapList = accountService.selectTop5CnameAndCount();
        return RespBean.ok("", mapList);

    }

    @GetMapping("/top")
    public RespBean DnameAndCount(HttpServletRequest request, HttpServletResponse response) {
        List<Map<String, Object>> mapList = accountService.selectCnameAndCount();
        return RespBean.ok("", mapList);

    }

    @GetMapping("/adminInfo")
    public RespBean getLoginInfo(@RequestHeader("token") String token) {

        Map<String, Object> map = JwtUtil.parseJwtToMap(token);
        Integer id = (Integer) map.get("id");

        Account account = accountService.selectById(id);

        Admin admin = adminService.selectByAid(account.getAid());

        ////获取Session中存放的已登录用户的信息
        //Object admin = session.getAttribute("admin");

        //返回
       return  RespBean.ok("", admin);
    }

}
